package org.mspring.mlog.config;

import freemarker.template.SimpleHash;
import freemarker.template.TemplateModelException;
import org.mspring.mlog.freemarker.directive.ArticleSummaryDirectiveModel;
import org.mspring.mlog.freemarker.directive.CategoryListDirectiveModel;
import org.mspring.mlog.freemarker.directive.HotArticleDirectiveModel;
import org.mspring.mlog.freemarker.directive.LatestArticleDirectiveModel;
import org.mspring.mlog.freemarker.directive.LinkListDirectiveModel;
import org.mspring.mlog.freemarker.directive.SiteTitleDirectiveModel;
import org.mspring.mlog.freemarker.directive.TagListDirectiveModel;
import org.mspring.mlog.freemarker.method.BaseConfigMethodModel;
import org.mspring.mlog.freemarker.method.ConfigMethodModel;
import org.mspring.mlog.interceptor.SettingInterceptor;
import org.mspring.mlog.interceptor.TabItemInterceptor;
import org.mspring.nbee.web.support.WebContextRequestMethodArgumentResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Gao Youbo
 * @since 2015-07-20 18:26
 */
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    private static final Logger LOG = LoggerFactory.getLogger(MvcConfig.class);

    @Autowired
    private SettingInterceptor settingInterceptor;
    @Autowired
    private TabItemInterceptor tabItemInterceptor;
    @Autowired
    private ConfigMethodModel configMethodModel;
    @Autowired
    private BaseConfigMethodModel baseConfigMethodModel;
    @Autowired
    private ArticleSummaryDirectiveModel articleSummaryDirectiveModel;
    @Autowired
    private LatestArticleDirectiveModel latestArticleDirectiveModel;
    @Autowired
    private HotArticleDirectiveModel hotArticleDirectiveModel;
    @Autowired
    private TagListDirectiveModel tagListDirectiveModel;
    @Autowired
    private LinkListDirectiveModel linkListDirectiveModel;
    @Autowired
    private CategoryListDirectiveModel categoryListDirectiveModel;
    @Autowired
    private SiteTitleDirectiveModel siteTitleDirectiveModel;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(settingInterceptor);
        registry.addInterceptor(tabItemInterceptor);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new WebContextRequestMethodArgumentResolver());
    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
        super.addReturnValueHandlers(returnValueHandlers);
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        Map<String, Object> freemarkerVariables = new HashMap<>();
        freemarkerVariables.put("config", configMethodModel);
        freemarkerVariables.put("base_config", baseConfigMethodModel);
        freemarkerVariables.put("article_summary", articleSummaryDirectiveModel);
        freemarkerVariables.put("latestArticles", latestArticleDirectiveModel);
        freemarkerVariables.put("hotArticles", hotArticleDirectiveModel);
        freemarkerVariables.put("tagList", tagListDirectiveModel);
        freemarkerVariables.put("linkList", linkListDirectiveModel);
        freemarkerVariables.put("categoryList", categoryListDirectiveModel);
        freemarkerVariables.put("siteTitle", siteTitleDirectiveModel);
        freemarker.template.Configuration config = freeMarkerConfigurer.getConfiguration();
        try {
            freeMarkerConfigurer.getConfiguration()
                    .setAllSharedVariables(new SimpleHash(freemarkerVariables, config.getObjectWrapper()));
        } catch (TemplateModelException e) {
            LOG.error(null, e);
        }
    }
}
